package com.iflysse.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;



import com.iflysse.dao.UserInfoDao;
import com.iflysse.pojo.MenuInfo;
import com.iflysse.pojo.UserInfo;
import com.iflysse.utils.MySessionFactory;
import com.iflysse.utils.PagerBean;



public class UserInfoService {
	private SqlSessionFactory sf;

	public UserInfoService() {
		sf = MySessionFactory.getSessionFactory();
	}

	public PagerBean<UserInfo> findUserPager(PagerBean<UserInfo> page) {
		SqlSession session = sf.openSession();
		try {
			UserInfoDao dao = (UserInfoDao) session
					.getMapper(UserInfoDao.class);
			List<UserInfo> userList = dao.findUserPager(page);
			long total = dao.findUserTotal();
			page.setRows(userList);
			page.setTotal(total);
		} finally {
			session.close();
		}
		return page;
	}

	/**
	 * 
	 * @author: Administrator
	 * @createTime: 2014年11月17日 下午3:32:36
	 * @history:
	 * @param userid
	 *            void
	 */
	public void delUserById(int userid) {
		SqlSession session = sf.openSession();
		try {
			UserInfoDao dao = (UserInfoDao) session
					.getMapper(UserInfoDao.class);
			dao.delById(userid);
			session.commit();
		} finally {
			session.close();
		}
	}

	/**
	*
	* @author: Administrator
	* @createTime: 2014年11月17日 下午3:55:08
	* @history:
	* @param userid
	* @return UserInfo
	*/
	public UserInfo findUserById(int userid) {
		SqlSession session = sf.openSession();
		UserInfo user=null;
		try {
			UserInfoDao dao = (UserInfoDao) session
					.getMapper(UserInfoDao.class);
			 user = dao.findUserbyId(userid);
		} finally {
			session.close();
		}
		return user;
	}
	public long findUserTotalById(int id){
		SqlSession session = sf.openSession();
		UserInfoDao dao = (UserInfoDao) session
				.getMapper(UserInfoDao.class);
		
		return dao.findUserTotalById(id);
		
	}
	public long findUserTotalByName(String name){
		SqlSession session = sf.openSession();
		UserInfoDao dao = (UserInfoDao) session
				.getMapper(UserInfoDao.class);
		
		return dao.findUserTotalByName(name);
	}
	public List<UserInfo > findUserByName(String userName) {
		SqlSession session = sf.openSession();
		List<UserInfo> user=null;
		try {
			UserInfoDao dao = (UserInfoDao) session
					.getMapper(UserInfoDao.class);
			 user = dao.findUserbyName(userName);
		} finally {
			session.close();
		}
		return user;
	}
	public void addUser(UserInfo user) {
		SqlSession session = sf.openSession();
		try {
			UserInfoDao dao = (UserInfoDao) session
					.getMapper(UserInfoDao.class);
			dao.insertUser(user);
			session.commit();
		} finally {
			session.close();
		}

	}
	public void updateUser(UserInfo user){
		SqlSession session = sf.openSession();
		try {
			UserInfoDao dao=session.getMapper(UserInfoDao.class);
			dao.updateUser(user);
			session.commit();
		} finally {
			session.close();
		}

		
	}
	public List<Integer> findRoleIdListByUserId(int id)
	{
		SqlSession session=sf.openSession();
		List<Integer> list=null;
		try{
			UserInfoDao dao=session.getMapper(UserInfoDao.class);
			list=dao.findRoleIdListByUserId(id);
		}finally{
			session.close();
		}
		return list;
	}
	
	public List<Integer> findMenuIdListByRoleId(int id)
	{
		SqlSession session=sf.openSession();
		List<Integer> list=null;
		try{
			UserInfoDao dao=session.getMapper(UserInfoDao.class);
			list=dao.findMenuIdListByRoleId(id);
		}finally{
			session.close();
		}
		return list;
		
	}
	public int getMenuParentIdByMenuId(int id)
	{
		SqlSession session=sf.openSession();
		int parentId=0;
		try{
			UserInfoDao dao=session.getMapper(UserInfoDao.class);
			parentId=dao.getMenuParentIdByMenuId(id);
		}finally{
			session.close();
		}
		return parentId;
	}
	public MenuInfo getMenuInoByMenuId(int id)
	{
		SqlSession session=sf.openSession();
		MenuInfo info=null;
		try{
			UserInfoDao dao=session.getMapper(UserInfoDao.class);
			info=dao.getMenuInfoByMenuId(id);
		}finally{
			session.close();
		}
		return info;
	}
	}
